package com.cencat.vehicle.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cencat.common.result.Result;
import com.cencat.vehicle.dto.*;
import com.cencat.vehicle.service.VehicleService;
import com.cencat.vehicle.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 车辆管理控制器
 *
 * @author cencat
 * @version 1.0
 * @since 2024-12-19
 */
@Slf4j
@RestController
@RequestMapping("/api/vehicle")
@RequiredArgsConstructor
@Tag(name = "车辆管理", description = "车辆相关接口")
public class VehicleController {

    private final VehicleService vehicleService;

    /**
     * 分页查询车辆列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询车辆列表")
    public Result<Page<VehicleVO>> getVehiclePage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Long size,
            @Parameter(description = "查询条件") VehicleQueryDTO query) {
        Page<VehicleVO> page = new Page<>(current, size);
        Page<VehicleVO> result = vehicleService.getVehiclePage(page, query);
        return Result.success(result);
    }

    /**
     * 根据ID获取车辆详情
     */
    @GetMapping("/{vehicleId}")
    @Operation(summary = "根据ID获取车辆详情")
    public Result<VehicleVO> getVehicleById(
            @Parameter(description = "车辆ID") @PathVariable Long vehicleId) {
        VehicleVO vehicle = vehicleService.getVehicleById(vehicleId);
        return Result.success(vehicle);
    }

    /**
     * 创建车辆
     */
    @PostMapping
    @Operation(summary = "创建车辆")
    public Result<Boolean> createVehicle(
            @Parameter(description = "创建参数") @Valid @RequestBody VehicleCreateDTO createDTO) {
        Boolean result = vehicleService.createVehicle(createDTO);
        return Result.success(result);
    }

    /**
     * 更新车辆
     */
    @PutMapping
    @Operation(summary = "更新车辆")
    public Result<Boolean> updateVehicle(
            @Parameter(description = "更新参数") @Valid @RequestBody VehicleUpdateDTO updateDTO) {
        Boolean result = vehicleService.updateVehicle(updateDTO);
        return Result.success(result);
    }

    /**
     * 删除车辆
     */
    @DeleteMapping("/{vehicleId}")
    @Operation(summary = "删除车辆")
    public Result<Boolean> deleteVehicle(
            @Parameter(description = "车辆ID") @PathVariable Long vehicleId) {
        Boolean result = vehicleService.deleteVehicle(vehicleId);
        return Result.success(result);
    }

    /**
     * 批量删除车辆
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除车辆")
    public Result<Boolean> batchDeleteVehicles(
            @Parameter(description = "车辆ID列表") @RequestBody List<Long> vehicleIds) {
        Boolean result = vehicleService.batchDeleteVehicles(vehicleIds);
        return Result.success(result);
    }

    /**
     * 更新车辆状态
     */
    @PutMapping("/{vehicleId}/status")
    @Operation(summary = "更新车辆状态")
    public Result<Boolean> updateVehicleStatus(
            @Parameter(description = "车辆ID") @PathVariable Long vehicleId,
            @Parameter(description = "车辆状态") @RequestParam Integer status) {
        Boolean result = vehicleService.updateVehicleStatus(vehicleId, status);
        return Result.success(result);
    }

    /**
     * 批量更新车辆状态
     */
    @PutMapping("/batch/status")
    @Operation(summary = "批量更新车辆状态")
    public Result<Boolean> batchUpdateVehicleStatus(
            @Parameter(description = "车辆ID列表") @RequestParam List<Long> vehicleIds,
            @Parameter(description = "车辆状态") @RequestParam Integer status) {
        Boolean result = vehicleService.batchUpdateVehicleStatus(vehicleIds, status);
        return Result.success(result);
    }

    /**
     * 获取车辆统计数据
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取车辆统计数据")
    public Result<VehicleStatisticsVO> getVehicleStatistics(
            @Parameter(description = "开始时间") @RequestParam LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam LocalDateTime endTime) {
        VehicleStatisticsVO statistics = vehicleService.getVehicleStatistics(startTime, endTime);
        return Result.success(statistics);
    }

    /**
     * 获取车辆趋势数据
     */
    @GetMapping("/trend")
    @Operation(summary = "获取车辆趋势数据")
    public Result<List<VehicleTrendVO>> getVehicleTrend(
            @Parameter(description = "天数") @RequestParam(defaultValue = "7") Integer days) {
        List<VehicleTrendVO> trend = vehicleService.getVehicleTrend(days);
        return Result.success(trend);
    }

    /**
     * 获取车辆排行榜
     */
    @GetMapping("/ranking")
    @Operation(summary = "获取车辆排行榜")
    public Result<List<VehicleRankingVO>> getVehicleRanking(
            @Parameter(description = "排行类型") @RequestParam Integer type,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<VehicleRankingVO> ranking = vehicleService.getVehicleRanking(type, limit);
        return Result.success(ranking);
    }

    /**
     * 获取车辆分布数据
     */
    @GetMapping("/distribution")
    @Operation(summary = "获取车辆分布数据")
    public Result<List<VehicleDistributionVO>> getVehicleDistribution(
            @Parameter(description = "分布类型") @RequestParam Integer type) {
        List<VehicleDistributionVO> distribution = vehicleService.getVehicleDistribution(type);
        return Result.success(distribution);
    }

    /**
     * 获取司机车辆列表
     */
    @GetMapping("/driver/{driverId}")
    @Operation(summary = "获取司机车辆列表")
    public Result<List<VehicleVO>> getDriverVehicles(
            @Parameter(description = "司机ID") @PathVariable Long driverId,
            @Parameter(description = "车辆状态") @RequestParam(required = false) Integer status) {
        List<VehicleVO> vehicles = vehicleService.getDriverVehicles(driverId, status);
        return Result.success(vehicles);
    }

    /**
     * 获取商户车辆列表
     */
    @GetMapping("/merchant/{merchantId}")
    @Operation(summary = "获取商户车辆列表")
    public Result<List<VehicleVO>> getMerchantVehicles(
            @Parameter(description = "商户ID") @PathVariable Long merchantId,
            @Parameter(description = "车辆状态") @RequestParam(required = false) Integer status) {
        List<VehicleVO> vehicles = vehicleService.getMerchantVehicles(merchantId, status);
        return Result.success(vehicles);
    }

    /**
     * 获取附近车辆
     */
    @GetMapping("/nearby")
    @Operation(summary = "获取附近车辆")
    public Result<List<VehicleVO>> getNearbyVehicles(
            @Parameter(description = "经度") @RequestParam Double longitude,
            @Parameter(description = "纬度") @RequestParam Double latitude,
            @Parameter(description = "半径(公里)") @RequestParam(defaultValue = "5.0") Double radius,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<VehicleVO> vehicles = vehicleService.getNearbyVehicles(longitude, latitude, radius, limit);
        return Result.success(vehicles);
    }

    /**
     * 智能车辆匹配
     */
    @PostMapping("/match")
    @Operation(summary = "智能车辆匹配")
    public Result<List<VehicleMatchVO>> smartVehicleMatch(
            @Parameter(description = "匹配参数") @Valid @RequestBody VehicleMatchDTO matchDTO) {
        List<VehicleMatchVO> matches = vehicleService.smartVehicleMatch(matchDTO);
        return Result.success(matches);
    }

    /**
     * 获取车辆轨迹
     */
    @GetMapping("/{vehicleId}/track")
    @Operation(summary = "获取车辆轨迹")
    public Result<List<VehicleTrackVO>> getVehicleTrack(
            @Parameter(description = "车辆ID") @PathVariable Long vehicleId,
            @Parameter(description = "开始时间") @RequestParam LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam LocalDateTime endTime) {
        List<VehicleTrackVO> track = vehicleService.getVehicleTrack(vehicleId, startTime, endTime);
        return Result.success(track);
    }

    /**
     * 更新车辆位置
     */
    @PutMapping("/location")
    @Operation(summary = "更新车辆位置")
    public Result<Boolean> updateVehicleLocation(
            @Parameter(description = "位置信息") @Valid @RequestBody VehicleLocationDTO locationDTO) {
        Boolean result = vehicleService.updateVehicleLocation(locationDTO);
        return Result.success(result);
    }

    /**
     * 获取车辆维护记录
     */
    @GetMapping("/{vehicleId}/maintenance")
    @Operation(summary = "获取车辆维护记录")
    public Result<List<VehicleMaintenanceVO>> getVehicleMaintenance(
            @Parameter(description = "车辆ID") @PathVariable Long vehicleId) {
        List<VehicleMaintenanceVO> maintenance = vehicleService.getVehicleMaintenance(vehicleId);
        return Result.success(maintenance);
    }

    /**
     * 添加车辆维护记录
     */
    @PostMapping("/maintenance")
    @Operation(summary = "添加车辆维护记录")
    public Result<Boolean> addVehicleMaintenance(
            @Parameter(description = "维护记录") @Valid @RequestBody VehicleMaintenanceDTO maintenanceDTO) {
        Boolean result = vehicleService.addVehicleMaintenance(maintenanceDTO);
        return Result.success(result);
    }

    /**
     * 获取车辆保险信息
     */
    @GetMapping("/{vehicleId}/insurance")
    @Operation(summary = "获取车辆保险信息")
    public Result<VehicleInsuranceVO> getVehicleInsurance(
            @Parameter(description = "车辆ID") @PathVariable Long vehicleId) {
        VehicleInsuranceVO insurance = vehicleService.getVehicleInsurance(vehicleId);
        return Result.success(insurance);
    }

    /**
     * 更新车辆保险信息
     */
    @PutMapping("/insurance")
    @Operation(summary = "更新车辆保险信息")
    public Result<Boolean> updateVehicleInsurance(
            @Parameter(description = "保险信息") @Valid @RequestBody VehicleInsuranceDTO insuranceDTO) {
        Boolean result = vehicleService.updateVehicleInsurance(insuranceDTO);
        return Result.success(result);
    }

    /**
     * 导出车辆数据
     */
    @PostMapping("/export")
    @Operation(summary = "导出车辆数据")
    public Result<VehicleExportVO> exportVehicles(
            @Parameter(description = "查询条件") @RequestBody VehicleQueryDTO query) {
        VehicleExportVO export = vehicleService.exportVehicles(query);
        return Result.success(export);
    }

    /**
     * 获取车辆详细信息
     */
    @GetMapping("/{vehicleId}/detail")
    @Operation(summary = "获取车辆详细信息")
    public Result<VehicleDetailVO> getVehicleDetail(
            @Parameter(description = "车辆ID") @PathVariable Long vehicleId) {
        VehicleDetailVO detail = vehicleService.getVehicleDetail(vehicleId);
        return Result.success(detail);
    }

    /**
     * 搜索车辆
     */
    @GetMapping("/search")
    @Operation(summary = "搜索车辆")
    public Result<List<VehicleVO>> searchVehicles(
            @Parameter(description = "搜索关键词") @RequestParam String keyword,
            @Parameter(description = "搜索类型") @RequestParam(defaultValue = "all") String searchType,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<VehicleVO> vehicles = vehicleService.searchVehicles(keyword, searchType, limit);
        return Result.success(vehicles);
    }

    /**
     * 获取推荐车辆
     */
    @GetMapping("/recommend/{driverId}")
    @Operation(summary = "获取推荐车辆")
    public Result<List<VehicleVO>> getRecommendedVehicles(
            @Parameter(description = "司机ID") @PathVariable Long driverId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<VehicleVO> vehicles = vehicleService.getRecommendedVehicles(driverId, limit);
        return Result.success(vehicles);
    }

    /**
     * 获取车辆性能数据
     */
    @GetMapping("/{vehicleId}/performance")
    @Operation(summary = "获取车辆性能数据")
    public Result<VehiclePerformanceVO> getVehiclePerformance(
            @Parameter(description = "车辆ID") @PathVariable Long vehicleId) {
        VehiclePerformanceVO performance = vehicleService.getVehiclePerformance(vehicleId);
        return Result.success(performance);
    }

    /**
     * 获取车辆优化建议
     */
    @GetMapping("/{vehicleId}/optimization")
    @Operation(summary = "获取车辆优化建议")
    public Result<List<String>> getVehicleOptimizationSuggestions(
            @Parameter(description = "车辆ID") @PathVariable Long vehicleId) {
        List<String> suggestions = vehicleService.getVehicleOptimizationSuggestions(vehicleId);
        return Result.success(suggestions);
    }

    /**
     * 检查车辆是否存在
     */
    @GetMapping("/exists")
    @Operation(summary = "检查车辆是否存在")
    public Result<Boolean> checkVehicleExists(
            @Parameter(description = "车牌号") @RequestParam String plateNumber) {
        Boolean exists = vehicleService.checkVehicleExists(plateNumber);
        return Result.success(exists);
    }

    /**
     * 获取相似车辆
     */
    @GetMapping("/{vehicleId}/similar")
    @Operation(summary = "获取相似车辆")
    public Result<List<VehicleVO>> getSimilarVehicles(
            @Parameter(description = "车辆ID") @PathVariable Long vehicleId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<VehicleVO> vehicles = vehicleService.getSimilarVehicles(vehicleId, limit);
        return Result.success(vehicles);
    }

    /**
     * 更新车辆评分
     */
    @PutMapping("/{vehicleId}/rating")
    @Operation(summary = "更新车辆评分")
    public Result<Boolean> updateVehicleRating(
            @Parameter(description = "车辆ID") @PathVariable Long vehicleId,
            @Parameter(description = "评分") @RequestParam Double rating) {
        Boolean result = vehicleService.updateVehicleRating(vehicleId, rating);
        return Result.success(result);
    }

}